package com.gitee.zeric.codec.service;

import com.gitee.zeric.codec.check.CodeUniqueCheck;
import com.gitee.zeric.codec.mapper.SysCodeRuleGenerateMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * @author zj
 * @date 2022/4/19
 */
@ConditionalOnProperty(prefix = "dps",name = "unichecker",havingValue = "mysql",matchIfMissing = true)
@Service(value = "codeUniqueCheck")
@RequiredArgsConstructor
public class MyCodeCheck implements CodeUniqueCheck {
    private final SysCodeRuleGenerateMapper sysCodeRuleGenerateMapper;

    @Override
    public boolean checkAndInsert(String ruleCode, String realCode) {
        boolean flag = false;
        try {
//            SysCodeRuleGenerate sysCodeRuleGenerate=new SysCodeRuleGenerate();
//            sysCodeRuleGenerate.setRuleCode(ruleCode);
//            sysCodeRuleGenerate.set
//            sysCodeRuleGenerateMapper.insert()
            if (sysCodeRuleGenerateMapper.insertCode(ruleCode, realCode) > 0) {
                flag = true;
            }
        } catch (DuplicateKeyException e) {
            //数据已存在
            flag = false;
        }
        return flag;
    }
}
